<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="${initParam.root }jquery-1.9.1.js"></script>
<script type="text/javascript">
	$(document).ready(function(){
		$("#newMessageBtn").click(function(){
			if($("form [name=receiverId]").val()==""){
				alert("받는사람 id를 입력해주세요");
				return false;
			}else if($("form [name=content]").val()==""){
				alert("내용을 입력해주세요");
				return false;
			}else{
				$.ajax({
					type:"get",
					url:"${initParam.root}message.do",
					dataType:"json",
					data:$("#newMessageForm").serialize(),
					success:function(jsonData){
						if(jsonData.result==-1){
							alert("메세지 보내기를 실패했습니다\n받는사람id가 존재하지 않습니다.");
						}else if(jsonData.result==1){
							alert("쪽지를 성공적으로 보냈습니다");
							$("#newMessageForm [name=receiverId]").val()=="";
							$("#newMessageForm [name=content]").val()=="";
						}else{
							alert("쪽지보내기를 실패했습니다\n잠시후 다시 시도해주세요");
						}
					}
				});
			}//보내기 버튼 클릭(빈칸 다 채웠으면)
		});//보내기 버튼 클릭시 정보 전송
		
	});
</script>
</head>
<body>
	<c:choose>
		<c:when test="${sessionScope.userInfo==null}">
			<script type="text/javascript">
				alert("로그인 후에 이용가능한 서비스입니다");
				location.href = "${initParam.root}member.do?command=home";
			</script>
		</c:when>
		<c:otherwise>
			<h3>쪽지보내기</h3><br>
				<form id="newMessageForm">
				받는사람: <input type="text" name="receiverId"><br>
				<textarea cols="50" rows="18" name="content"></textarea><br>
				<input type="hidden" name="command" value="newMessage">
				<input type="button" value="보내기" id="newMessageBtn">
				</form>
		</c:otherwise>
	</c:choose>
</body>
</html>